﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JamesThew.Dashboard;

namespace JamesThew
{
    public partial class register : System.Web.UI.Page
    {
        public UserDB udb;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UserPaymentMethod_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            this.udb = new UserDB();
            bool RegisterStatus;

            String UserLogin = this.UserLogin.Text;
            String UserPass = this.UserPass.Text;
            String UserConfirmPass = this.UserConfirmPass.Text;
            String UserEmail = this.UserEmail.Text;
            String UserPlan = this.UserPlan.SelectedValue;
            String UserPaymentMethod = this.UserPaymentMethod.SelectedValue;
            int nBalance;


            if (!UserPass.Equals(UserConfirmPass))
            {
                Response.Write("Confirm password did not match!");
                return;
            }
            if (JamesThew.Includes.Core.IsValidEmail(UserEmail) == false)
            {
                Response.Write("Incorrect email");
                return;
            }

            if ( UserPaymentMethod.Equals("credit_card") ){
                String nCreditCardHolder = this.CreditCardHolder.Text;
                String nCreditCardNumber = this.CreditCardNumber.Text;
                int nCCV = JamesThew.Includes.Core.ParseInt(this.CCV.Text);
                int nExpiredMonth = JamesThew.Includes.Core.ParseInt(this.ExpiredMonth.Text);
                int nExpiredYear = JamesThew.Includes.Core.ParseInt(this.ExpiredYear.Text);

                if (UserPlan.Equals("monthy"))
                    nBalance = 10;
                else
                    nBalance = 100;

                Bank.Bank bank = new Bank.Bank();
                String PaymentStatus = bank.BuyItem(nCreditCardNumber, nCreditCardHolder, nCCV, nExpiredMonth, nExpiredYear, nBalance);
                if (PaymentStatus.Equals("success"))
                {
                    RegisterStatus = this.udb.Register(UserLogin, UserPass, UserEmail, UserPaymentMethod, UserPlan);
                    if (RegisterStatus == true)
                    {
                        Response.Redirect("Thankyou.aspx");
                    }
                    else
                    {
                        Response.Write("Error");
                    }
                }
                else
                    Response.Write("Error!");
            }

            if (UserPaymentMethod.Equals("bank_transfer") || UserPaymentMethod.Equals("direct")){
                 RegisterStatus = this.udb.Register(UserLogin, UserPass, UserEmail, UserPaymentMethod, UserPlan);
                    if (RegisterStatus == true)
                    {
                        Response.Redirect("Thankyou.aspx");
                    }
                    else
                    {
                        Response.Write("Error");
                    }
            }

        }
    }
}